package com.lintf.wxpay.base.utils;

import com.lintf.common.utils.DateUtils;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lin tingfeng
 * @system Mac
 * @data 2024/11/21 14:02
 * @package_name com.lintf.wxpay.base.utils
 * @project_name sharding-sphere
 * @desc
 */
public class OrderNoUtils {
    // 静态变量，用于存储当前日期
    private static String currentDate = null;

    // 静态变量，用于存储递增的序列号
    private static AtomicInteger sequence = new AtomicInteger(0);

    public static String getOrderNo() {
        return "ORDER_" + getNo();
    }

    public static String getRefundNo() {
        return "REFUND_" + getNo();
    }

    private static synchronized String getNo() {
        // 获取当前日期
        String dateTimeNow = DateUtils.dateTimeNow();

        // 如果当前日期发生变化，重置序列号
        if (!dateTimeNow.equals(currentDate)) {
            currentDate = dateTimeNow;
            sequence.set(0);
        }

        // 生成四位数的序列号
        int seq = sequence.incrementAndGet();
        String sequenceStr = String.format("%04d", seq);

        // 返回最终的订单号
        return dateTimeNow + sequenceStr;

    }

}
